/////////////////////////////////////////////////////////////////////////////
// Name:        commandeditorpanel.cpp
// Purpose:     
// Author:      Julian Smart
// Modified by: 
// Created:     22/11/2008 13:32:13
// RCS-ID:      
// Copyright:   (c) Anthemion Software Limited
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

////@begin includes
////@end includes

#include "commandeditor/commandeditorpanel.h"
#include "commandeditor/commandeditor.h"
#include "commandeditor/commandeditordialog.h"
#include "commandeditor/commandeditortypedialog.h"

////@begin XPM images
////@end XPM images

#include "commandeditor/bitmaps/addnote.xpm"
#include "commandeditor/bitmaps/deletenote.xpm"
#include "commandeditor/bitmaps/editproperties16x16.xpm"

wxUICustomization* wxCommandEditorPanel::m_uiCustomization = NULL;
bool wxCommandEditorPanel::sm_useTooltips = true;


/*
 * wxCommandEditorPanel type definition
 */

IMPLEMENT_DYNAMIC_CLASS( wxCommandEditorPanel, wxPanel )


/*
 * wxCommandEditorPanel event table definition
 */

BEGIN_EVENT_TABLE( wxCommandEditorPanel, wxPanel )

////@begin wxCommandEditorPanel event table entries
    EVT_COMBOBOX( ID_COMMAND_EDITOR_FILE_TYPES, wxCommandEditorPanel::OnCommandEditorFileTypesSelected )

    EVT_BUTTON( ID_COMMAND_EDITOR_ADD_TYPE, wxCommandEditorPanel::OnCommandEditorAddTypeClick )

    EVT_BUTTON( ID_COMMAND_EDITOR_DELETE_TYPE, wxCommandEditorPanel::OnCommandEditorDeleteTypeClick )
    EVT_UPDATE_UI( ID_COMMAND_EDITOR_DELETE_TYPE, wxCommandEditorPanel::OnCommandEditorDeleteTypeUpdate )

    EVT_BUTTON( ID_COMMAND_EDITOR_EDIT_TYPE, wxCommandEditorPanel::OnCommandEditorEditTypeClick )
    EVT_UPDATE_UI( ID_COMMAND_EDITOR_EDIT_TYPE, wxCommandEditorPanel::OnCommandEditorEditTypeUpdate )

    EVT_COMBOBOX( ID_COMMAND_EDITOR_PANEL_VIEWER_COMBOCTRL, wxCommandEditorPanel::OnCommandEditorPanelViewerComboctrlSelected )

    EVT_BUTTON( ID_COMMAND_EDITOR_PANEL_VIEWER_EDIT, wxCommandEditorPanel::OnCommandEditorPanelViewerEditClick )
    EVT_UPDATE_UI( ID_COMMAND_EDITOR_PANEL_VIEWER_EDIT, wxCommandEditorPanel::OnCommandEditorPanelViewerEditUpdate )

    EVT_COMBOBOX( ID_COMMAND_EDITOR_PANEL_EDITOR_COMBOCTRL, wxCommandEditorPanel::OnCommandEditorPanelEditorComboctrlSelected )

    EVT_BUTTON( ID_COMMAND_EDITOR_PANEL_EDITOR_EDIT, wxCommandEditorPanel::OnCommandEditorPanelEditorEditClick )
    EVT_UPDATE_UI( ID_COMMAND_EDITOR_PANEL_EDITOR_EDIT, wxCommandEditorPanel::OnCommandEditorPanelEditorEditUpdate )

////@end wxCommandEditorPanel event table entries

END_EVENT_TABLE()


/*
 * wxCommandEditorPanel constructors
 */

wxCommandEditorPanel::wxCommandEditorPanel()
{
    Init();
}

wxCommandEditorPanel::wxCommandEditorPanel( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, pos, size, style);
}


/*
 * wxCommandEditorPanel creator
 */

bool wxCommandEditorPanel::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
{
////@begin wxCommandEditorPanel creation
    SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY);
    wxPanel::Create( parent, id, pos, size, style );

    CreateControls();
    if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);
    }
    Centre();
////@end wxCommandEditorPanel creation
    return true;
}


/*
 * wxCommandEditorPanel destructor
 */

wxCommandEditorPanel::~wxCommandEditorPanel()
{
////@begin wxCommandEditorPanel destruction
////@end wxCommandEditorPanel destruction
}


/*
 * Member initialisation
 */

void wxCommandEditorPanel::Init()
{
    m_commandManager = NULL;
    m_noUpdates = false;
    sm_useTooltips = true;
    
////@begin wxCommandEditorPanel member initialisation
    m_commandEditorFileTypeCombo = NULL;
    m_addDeleteTypeSizer = NULL;
    m_flexGridSizer = NULL;
    m_commandEditorViewerCombo = NULL;
    m_commandEditorViewerEditCtrl = NULL;
    m_editorsLabelCtrl = NULL;
    m_commandEditorEditorCombo = NULL;
    m_commandEditorEditorEditCtrl = NULL;
////@end wxCommandEditorPanel member initialisation
}


/*
 * Control creation for ebCommandEditorPanel
 */

void wxCommandEditorPanel::CreateControls()
{    
////@begin wxCommandEditorPanel content construction
    wxCommandEditorPanel* itemPanel1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemPanel1->SetSizer(itemBoxSizer2);

    wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer2->Add(itemBoxSizer3, 1, wxGROW, 5);

    wxStaticText* itemStaticText4 = new wxStaticText( itemPanel1, wxID_STATIC, _("&File types:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer3->Add(itemStaticText4, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP, 5);

    wxBoxSizer* itemBoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer3->Add(itemBoxSizer5, 0, wxGROW|wxRIGHT, 5);

    wxArrayString m_commandEditorFileTypeComboStrings;
    m_commandEditorFileTypeCombo = new wxComboBox( itemPanel1, ID_COMMAND_EDITOR_FILE_TYPES, _T(""), wxDefaultPosition, wxDefaultSize, m_commandEditorFileTypeComboStrings, wxCB_READONLY );
    m_commandEditorFileTypeCombo->SetHelpText(_("Choose a file type to configure."));
    if (wxCommandEditorPanel::ShowToolTips())
        m_commandEditorFileTypeCombo->SetToolTip(_("Choose a file type to configure."));
    itemBoxSizer5->Add(m_commandEditorFileTypeCombo, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_addDeleteTypeSizer = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer5->Add(m_addDeleteTypeSizer, 0, wxALIGN_CENTER_VERTICAL, 5);

    wxBitmapButton* itemBitmapButton8 = new wxBitmapButton( itemPanel1, ID_COMMAND_EDITOR_ADD_TYPE, itemPanel1->GetBitmapResource(wxT("bitmaps/addnote.xpm")), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW );
    itemBitmapButton8->SetHelpText(_("Add a new file type."));
    if (wxCommandEditorPanel::ShowToolTips())
        itemBitmapButton8->SetToolTip(_("Add a new file type."));
    m_addDeleteTypeSizer->Add(itemBitmapButton8, 0, wxALIGN_CENTER_VERTICAL|wxTOP|wxBOTTOM, 5);

    wxBitmapButton* itemBitmapButton9 = new wxBitmapButton( itemPanel1, ID_COMMAND_EDITOR_DELETE_TYPE, itemPanel1->GetBitmapResource(wxT("bitmaps/deletenote.xpm")), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW );
    itemBitmapButton9->SetHelpText(_("Delete the selected file type."));
    if (wxCommandEditorPanel::ShowToolTips())
        itemBitmapButton9->SetToolTip(_("Delete the selected file type."));
    m_addDeleteTypeSizer->Add(itemBitmapButton9, 0, wxALIGN_CENTER_VERTICAL|wxTOP|wxBOTTOM, 5);

    wxBitmapButton* itemBitmapButton10 = new wxBitmapButton( itemPanel1, ID_COMMAND_EDITOR_EDIT_TYPE, itemPanel1->GetBitmapResource(wxT("bitmaps/editproperties16x16.xpm")), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW );
    itemBitmapButton10->SetHelpText(_("Edit the selected file type."));
    if (wxCommandEditorPanel::ShowToolTips())
        itemBitmapButton10->SetToolTip(_("Edit the selected file type."));
    m_addDeleteTypeSizer->Add(itemBitmapButton10, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxTOP|wxBOTTOM, 5);

    wxBoxSizer* itemBoxSizer11 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer3->Add(itemBoxSizer11, 0, wxGROW, 5);

    itemBoxSizer11->Add(5, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_flexGridSizer = new wxFlexGridSizer(0, 3, 0, 0);
    m_flexGridSizer->AddGrowableCol(1);
    itemBoxSizer11->Add(m_flexGridSizer, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText14 = new wxStaticText( itemPanel1, wxID_STATIC, _("&Viewers:"), wxDefaultPosition, wxDefaultSize, 0 );
    m_flexGridSizer->Add(itemStaticText14, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxArrayString m_commandEditorViewerComboStrings;
    m_commandEditorViewerCombo = new wxComboBox( itemPanel1, ID_COMMAND_EDITOR_PANEL_VIEWER_COMBOCTRL, _T(""), wxDefaultPosition, wxSize(160, -1), m_commandEditorViewerComboStrings, wxCB_READONLY );
    m_commandEditorViewerCombo->SetHelpText(_("Viewer commands for the current file type."));
    if (wxCommandEditorPanel::ShowToolTips())
        m_commandEditorViewerCombo->SetToolTip(_("Viewer commands for the current file type."));
    m_flexGridSizer->Add(m_commandEditorViewerCombo, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_commandEditorViewerEditCtrl = new wxButton( itemPanel1, ID_COMMAND_EDITOR_PANEL_VIEWER_EDIT, _("Configure"), wxDefaultPosition, wxDefaultSize, 0 );
    m_commandEditorViewerEditCtrl->SetHelpText(_("Click to configure viewers for the selected file type."));
    if (wxCommandEditorPanel::ShowToolTips())
        m_commandEditorViewerEditCtrl->SetToolTip(_("Click to configure viewers for the selected file type."));
    m_flexGridSizer->Add(m_commandEditorViewerEditCtrl, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_editorsLabelCtrl = new wxStaticText( itemPanel1, wxID_STATIC, _("&Editors:"), wxDefaultPosition, wxDefaultSize, 0 );
    m_flexGridSizer->Add(m_editorsLabelCtrl, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxArrayString m_commandEditorEditorComboStrings;
    m_commandEditorEditorCombo = new wxComboBox( itemPanel1, ID_COMMAND_EDITOR_PANEL_EDITOR_COMBOCTRL, _T(""), wxDefaultPosition, wxSize(160, -1), m_commandEditorEditorComboStrings, wxCB_READONLY );
    m_commandEditorEditorCombo->SetHelpText(_("Editor commands for the current file type."));
    if (wxCommandEditorPanel::ShowToolTips())
        m_commandEditorEditorCombo->SetToolTip(_("Editor commands for the current file type."));
    m_flexGridSizer->Add(m_commandEditorEditorCombo, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_commandEditorEditorEditCtrl = new wxButton( itemPanel1, ID_COMMAND_EDITOR_PANEL_EDITOR_EDIT, _("Configure"), wxDefaultPosition, wxDefaultSize, 0 );
    m_commandEditorEditorEditCtrl->SetHelpText(_("Click to configure editors for the selected file type."));
    if (wxCommandEditorPanel::ShowToolTips())
        m_commandEditorEditorEditCtrl->SetToolTip(_("Click to configure editors for the selected file type."));
    m_flexGridSizer->Add(m_commandEditorEditorEditCtrl, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

////@end wxCommandEditorPanel content construction

    if (GetWindowStyle() & wxCOMMAND_EDITOR_VIEWERS_ONLY)
    {
        m_flexGridSizer->Show(m_editorsLabelCtrl, false);
        m_flexGridSizer->Show(m_commandEditorEditorCombo, false);
        m_flexGridSizer->Show(m_commandEditorEditorEditCtrl, false);
    }
}


/*
 * Should we show tooltips?
 */

bool wxCommandEditorPanel::ShowToolTips()
{
    if (m_uiCustomization)
        return m_uiCustomization->UseToolTips();
    else
        return true;
}

/*
 * Get bitmap resources
 */

wxBitmap wxCommandEditorPanel::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin wxCommandEditorPanel bitmap retrieval
    wxUnusedVar(name);
    if (name == _T("bitmaps/addnote.xpm"))
    {
        wxBitmap bitmap(addnote_xpm);
        return bitmap;
    }
    else if (name == _T("bitmaps/deletenote.xpm"))
    {
        wxBitmap bitmap(deletenote_xpm);
        return bitmap;
    }
    else if (name == _T("bitmaps/editproperties16x16.xpm"))
    {
        wxBitmap bitmap(editproperties16x16_xpm);
        return bitmap;
    }
    return wxNullBitmap;
////@end wxCommandEditorPanel bitmap retrieval
}

/*
 * Get icon resources
 */

wxIcon wxCommandEditorPanel::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin wxCommandEditorPanel icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end wxCommandEditorPanel icon retrieval
}

/// Populate drop-down list of file types
void wxCommandEditorPanel::PopulateFileTypes(int sel)
{
    m_commandEditorFileTypeCombo->Clear();
    size_t i;
    for (i = 0; i < m_commandManager->GetFileTypes().GetCount(); i++)
    {
        const wxEditorCommands& cmds = m_commandManager->GetFileTypes()[i];
        m_commandEditorFileTypeCombo->Append(wxString(wxGetTranslation(cmds.m_fileTypeName)) + wxT(" (") + cmds.GetFileTypeExtensions() + wxT(")"));
    }

    if (m_commandEditorFileTypeCombo->GetCount() > 0)
    {
        if (sel == -1)
            sel = 0;
        m_commandEditorFileTypeCombo->SetSelection(sel);
        ShowFileType(sel);
    }
}

/// Show a particular file type
void wxCommandEditorPanel::ShowFileType(int i)
{
    m_noUpdates = true;

    m_commandEditorViewerCombo->Clear();
    m_commandEditorEditorCombo->Clear();
    
    if (i == -1)
    {
    
    }
    else
    {
        const wxEditorCommands& cmds = m_commandManager->GetFileTypes()[i];
        wxArrayString viewerCmdNames = cmds.GetViewerCommandNames(false);
        wxArrayString editorCmdNames = cmds.GetEditorCommandNames(false);
        m_commandEditorViewerCombo->Append(viewerCmdNames);
        m_commandEditorEditorCombo->Append(editorCmdNames);
        
        if (m_commandEditorViewerCombo->FindString(cmds.GetDefaultViewerName()) != -1)
            m_commandEditorViewerCombo->SetStringSelection(cmds.GetDefaultViewerName());
        else
            m_commandEditorViewerCombo->SetSelection(0);

        if (m_commandEditorEditorCombo->FindString(cmds.GetDefaultEditorName()) != -1)
            m_commandEditorEditorCombo->SetStringSelection(cmds.GetDefaultEditorName());
        else
            m_commandEditorEditorCombo->SetSelection(0);
    }

    m_noUpdates = false;
}

/*
 * wxEVT_COMMAND_COMBOBOX_SELECTED event handler for ID_COMMAND_EDITOR_FILE_TYPES
 */

void wxCommandEditorPanel::OnCommandEditorFileTypesSelected( wxCommandEvent& event )
{
    ShowFileType(event.GetSelection());
}


/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_COMMAND_EDITOR_PANEL_EDITOR_EDIT
 */

void wxCommandEditorPanel::OnCommandEditorPanelEditorEditClick( wxCommandEvent& WXUNUSED(event) )
{
    int sel = m_commandEditorFileTypeCombo->GetSelection();
    if (sel != -1)
    {
        wxEditorCommands& cmds = m_commandManager->GetFileTypes()[sel];
        wxString typeName = cmds.GetFileTypeName();
        wxString translatedName = wxGetTranslation(typeName);
        wxString msg(wxString::Format(_("Editors for %s:"), translatedName.c_str()));
        wxCommandEditorDialog dialog(cmds.GetEditors(), msg, wxGetTopLevelParent(this));
        dialog.SetUseTooltips(sm_useTooltips);
        dialog.SetUICustomization(m_uiCustomization);

        if (dialog.ShowModal() == wxID_OK)
        {
            ShowFileType(-1);

            cmds.GetEditors() = dialog.GetCommands();

            ShowFileType(sel);
        }
    }
}

/*
 * wxEVT_UPDATE_UI event handler for ID_COMMAND_EDITOR_PANEL_EDITOR_EDIT
 */

void wxCommandEditorPanel::OnCommandEditorPanelEditorEditUpdate( wxUpdateUIEvent& event )
{
    event.Enable(m_commandEditorFileTypeCombo->GetSelection() != -1);
}


/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_COMMAND_EDITOR_PANEL_VIEWER_EDIT
 */

void wxCommandEditorPanel::OnCommandEditorPanelViewerEditClick( wxCommandEvent& WXUNUSED(event) )
{
    int sel = m_commandEditorFileTypeCombo->GetSelection();
    if (sel != -1)
    {
        wxEditorCommands& cmds = m_commandManager->GetFileTypes()[sel];
        wxString typeName = cmds.GetFileTypeName();
        wxString translatedName = wxGetTranslation(typeName);
        wxString msg(wxString::Format(_("Viewers for %s:"), translatedName.c_str()));
        wxCommandEditorDialog dialog(cmds.GetViewers(), msg, wxGetTopLevelParent(this));
        dialog.SetUseTooltips(sm_useTooltips);
        dialog.SetUICustomization(m_uiCustomization);
        if (dialog.ShowModal() == wxID_OK)
        {
            ShowFileType(-1);

            cmds.GetViewers() = dialog.GetCommands();

            ShowFileType(sel);
        }
    }
}

/*
 * wxEVT_UPDATE_UI event handler for ID_COMMAND_EDITOR_PANEL_VIEWER_EDIT
 */

void wxCommandEditorPanel::OnCommandEditorPanelViewerEditUpdate( wxUpdateUIEvent& event )
{
    event.Enable(m_commandEditorFileTypeCombo->GetSelection() != -1);
}


/*
 * wxEVT_COMMAND_COMBOBOX_SELECTED event handler for ID_COMMAND_EDITOR_PANEL_VIEWER_COMBOCTRL
 */

void wxCommandEditorPanel::OnCommandEditorPanelViewerComboctrlSelected( wxCommandEvent& WXUNUSED(event) )
{
    if (m_noUpdates)
        return;

    int fileTypeSel = m_commandEditorFileTypeCombo->GetSelection();
    int cmdSel = m_commandEditorViewerCombo->GetSelection();
    if (fileTypeSel != -1 && cmdSel != -1)
    {
        wxEditorCommands& cmds = m_commandManager->GetFileTypes()[fileTypeSel];
        cmds.m_defaultViewerName = m_commandEditorViewerCombo->GetString(cmdSel);
    }
}


/*
 * wxEVT_COMMAND_COMBOBOX_SELECTED event handler for ID_COMMAND_EDITOR_PANEL_EDITOR_COMBOCTRL
 */

void wxCommandEditorPanel::OnCommandEditorPanelEditorComboctrlSelected( wxCommandEvent& WXUNUSED(event) )
{
    if (m_noUpdates)
        return;

    int fileTypeSel = m_commandEditorFileTypeCombo->GetSelection();
    int cmdSel = m_commandEditorEditorCombo->GetSelection();
    if (fileTypeSel != -1 && cmdSel != -1)
    {
        wxEditorCommands& cmds = m_commandManager->GetFileTypes()[fileTypeSel];
        cmds.m_defaultEditorName = m_commandEditorEditorCombo->GetString(cmdSel);
    }
}


/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_COMMAND_EDITOR_ADD_TYPE
 */

void wxCommandEditorPanel::OnCommandEditorAddTypeClick( wxCommandEvent& WXUNUSED(event) )
{
    wxEditorCommands commands;
    wxCommandEditorTypeDialog dialog(commands, wxGetTopLevelParent(this));
    if (dialog.ShowModal() == wxID_OK)
    {
        commands = dialog.GetCommands();

        if (commands.m_fileTypeName.IsEmpty())
            return;

        if (m_commandManager->GetFileType(commands.m_fileTypeName))
        {
            wxMessageBox(wxString::Format(_("Sorry, a type with this name already exists."), commands.m_fileTypeName.c_str()), _("Add File Type"), wxICON_EXCLAMATION|wxOK, wxGetTopLevelParent(this));
            return;
        }

        if (commands.m_fileTypeExtensionForDefaultViewing.IsEmpty())
            commands.m_fileTypeExtensionForDefaultViewing = commands.m_fileTypeExtensions.BeforeFirst(wxT(';'));
        if (commands.m_fileTypeExtensionForDefaultEditing.IsEmpty())
            commands.m_fileTypeExtensionForDefaultEditing = commands.m_fileTypeExtensions.BeforeFirst(wxT(';'));

        commands.InitializeDefaults(commands.m_fileTypeExtensionForDefaultViewing, commands.m_fileTypeExtensionForDefaultEditing, false /* no internal command */);

        m_commandManager->AddFileType(commands);
        m_commandEditorFileTypeCombo->Append(wxString(wxGetTranslation(commands.m_fileTypeName)) + wxT(" (") + commands.GetFileTypeExtensions() + wxT(")"));
    }
}


/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_COMMAND_EDITOR_DELETE_TYPE
 */

void wxCommandEditorPanel::OnCommandEditorDeleteTypeClick( wxCommandEvent& WXUNUSED(event) )
{
    int sel = m_commandEditorFileTypeCombo->GetSelection();
    if (sel != -1)
    {
        wxEditorCommands& cmds = m_commandManager->GetFileTypes()[sel];
        wxString typeName = cmds.GetFileTypeName();
        wxString translatedName = wxGetTranslation(typeName);
        int ans = wxMessageBox(wxString::Format(_("Delete file type %s?"), translatedName.c_str()), _("Delete File Type"), wxICON_QUESTION|wxYES_NO, wxGetTopLevelParent(this));
        if (ans == wxNO)
            return;

        m_commandManager->GetFileTypes().RemoveAt(sel);
        m_commandEditorFileTypeCombo->Delete(sel);
        if (m_commandEditorFileTypeCombo->GetCount())
            m_commandEditorFileTypeCombo->SetSelection(0);
    }
}


/*
 * wxEVT_UPDATE_UI event handler for ID_COMMAND_EDITOR_DELETE_TYPE
 */

void wxCommandEditorPanel::OnCommandEditorDeleteTypeUpdate( wxUpdateUIEvent& event )
{
    event.Enable(m_commandEditorFileTypeCombo->GetSelection() != -1);
}


/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_COMMAND_EDITOR_EDIT_TYPE
 */

void wxCommandEditorPanel::OnCommandEditorEditTypeClick( wxCommandEvent& WXUNUSED(event) )
{
    int sel = m_commandEditorFileTypeCombo->GetSelection();
    if (sel != -1)
    {
        wxEditorCommands commands = m_commandManager->GetFileTypes()[sel];
        wxString oldTypeName = commands.m_fileTypeName;
        wxCommandEditorTypeDialog dialog(commands, wxGetTopLevelParent(this));
        if (dialog.ShowModal() == wxID_OK)
        {
            commands = dialog.GetCommands();
            
            if (commands.m_fileTypeName.IsEmpty())
                return;
            
            if (commands.m_fileTypeName != oldTypeName && m_commandManager->GetFileType(commands.m_fileTypeName))
            {
                wxMessageBox(wxString::Format(_("Sorry, a type with this name already exists."), commands.m_fileTypeName.c_str()), _("Edit File Type"), wxICON_EXCLAMATION|wxOK, wxGetTopLevelParent(this));
                return;
            }
            
            if (commands.m_fileTypeExtensionForDefaultViewing.IsEmpty())
                commands.m_fileTypeExtensionForDefaultViewing = commands.m_fileTypeExtensions.BeforeFirst(wxT(';'));
            if (commands.m_fileTypeExtensionForDefaultEditing.IsEmpty())
                commands.m_fileTypeExtensionForDefaultEditing = commands.m_fileTypeExtensions.BeforeFirst(wxT(';'));
            
            m_commandManager->GetFileTypes()[sel] = commands;

            m_noUpdates = true;
            PopulateFileTypes(sel);
            m_noUpdates = false;
        }
    }
}


/*
 * wxEVT_UPDATE_UI event handler for ID_COMMAND_EDITOR_EDIT_TYPE
 */

void wxCommandEditorPanel::OnCommandEditorEditTypeUpdate( wxUpdateUIEvent& event )
{
    event.Enable(m_commandEditorFileTypeCombo->GetSelection() != -1);
}

